package com.tkgarment.system.action;
 
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
import com.tkgarment.system.dao.EmployeeDAO;
import com.tkgarment.system.factory.DAOFactory;
 
public class FileUploadAction extends ActionSupport implements
        ServletRequestAware {
    private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    private String empId;
    private HttpServletRequest servletRequest;
  
    
    public String execute() {
        try {
             String filePath = "d:/tksystem/emp_images/";
//            String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
            System.out.println("Server path:" + filePath);
            File fileToCreate = new File(filePath, this.userImageFileName);
              if(!fileToCreate.exists()){
                fileToCreate.mkdirs();
            }
            FileUtils.copyFile(this.userImage, fileToCreate);
            
            EmployeeDAO eDao = DAOFactory.createEmployeeDao();
             eDao.updatePic(empId,this.userImageFileName);
        } catch (Exception e) {
            e.printStackTrace();
            
            addActionError(e.getMessage());
 
            return INPUT;
        }
        return SUCCESS;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

  
    
    
    public File getUserImage() {
        return userImage;
    }
 
    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }
 
    public String getUserImageContentType() {
        return userImageContentType;
    }
 
    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }
 
    public String getUserImageFileName() {
        return userImageFileName;
    }
 
    public void setUserImageFileName(String userImageFileName) {
        this.userImageFileName = userImageFileName;
    }
 
    @Override
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
 
    }
}